﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.OleDb;
using System.Data.SqlTypes;

namespace DATN.GroupCustomer
{
    public partial class CreateGroup : System.Web.UI.Page
    {
        private string cnnString;
        SqlConnection cnn;
        SqlCommand cmd;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //Check tên nhóm khách hàng trùng nhau
        //input vào là tên. trả lại 1 or 0
        int CkName = 0;
        protected int checkName(string Name)
        {
            CkName = 0;
            try
            {
                string cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ConnectionString;
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                String queryString = "SELECT Count(*) FROM GroupCustomer WHERE Name = @Name";

                cmd = new SqlCommand(queryString, cnn);
                cmd.Parameters.Add("@Name", SqlDbType.VarChar);
                cmd.Parameters["@Name"].Value = Name;

                CkName = Convert.ToInt32(cmd.ExecuteScalar().ToString());

                cnn.Close();
            }
            catch (Exception ex)
            {
                lb_Status.Text = " Lỗi ở Check User ID: " + ex;
                cnn.Close();
            }

            return CkName;
        }

        

        // Tạo mới một GroupCustomer
        protected void CreatGrCus()
        {
            if (Session["Us_Id"] != null)
            {
                if (checkName(tbx_Name.Text) != 1)
                {
                    //Do something here!
                    int us_id = Convert.ToInt16(Session["Us_Id"].ToString());

                    try
                    {
                        cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                        cnn = new SqlConnection(cnnString);
                        cnn.Open();
                        cmd = cnn.CreateCommand();
                        cmd.CommandText = "INSERT INTO [dbo].[GroupCustomer] ([Name], [Description], [Us_Id])"
                                        + " VALUES (@Name, @Description, @Us_Id)";

                        cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
                        cmd.Parameters.Add("@Description", SqlDbType.NVarChar);
                        cmd.Parameters.Add("@Us_Id", SqlDbType.Int);

                        cmd.Parameters["@Name"].Value = tbx_Name.Text;
                        cmd.Parameters["@Description"].Value = tbx_des.Text;
                        cmd.Parameters["@Us_Id"].Value = us_id;

                        cmd.ExecuteNonQuery();

                        Response.Write("Lưu thành công");
                        cnn.Close();
                    }

                    catch (Exception ex)
                    {
                        Response.Write(ex);
                    }
                }
                else
                {
                    lb_Status.Text = "Tên nhóm đã tồn tại!";
                }
            }
            else
            {
                Response.Redirect("../Account/Login.aspx");
            }
        }


        //Click vào Button Tạo Nhóm:
        protected void btnCreateGroup_Click(object sender, EventArgs e)
        {
            if (tbx_Name.Text != null && tbx_Name.Text != "" && tbx_des.Text != null && tbx_des.Text != "")
            {
                //Tạo Nhóm Mới
                CreatGrCus();
                Response.Redirect("../GroupCustomer/GroupIndex.aspx");
            }
            else
            {
                lb_Status.Text = "Nên nhập đủ vào 2 ô text";
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../GroupCustomer/GroupIndex.aspx");
        }

        protected void btnGroupMM_Click(object sender, EventArgs e)
        {
            Response.Redirect("../GroupCustomer/GroupMM.aspx");
        }
    }
}